package cm367;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;

@ManagedBean
@SessionScoped
public class Request {

	private String name;
	private String email;
	private String role;
	private String password;
	private String rpassword;
	private String title;
	private String telNo;
	
	@ManagedProperty(value="#{requestService}")
	private RequestService requestService;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getRpassword() {
		return rpassword;
	}

	public void setRpassword(String rpassword) {
		this.rpassword = rpassword;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getTelNo() {
		return telNo;
	}

	public void setTelNo(String telNo) {
		this.telNo = telNo;
	}

	public RequestService getRequestService() {
		return requestService;
	}

	public void setRequestService(RequestService requestService) {
		this.requestService = requestService;
	}

	public void validateName(FacesContext context, UIComponent component,
			Object convertedValue) {
		String name = (String) convertedValue;
		if (requestService.nameExists(name)) {
			throw new ValidatorException(new FacesMessage(
					FacesMessage.SEVERITY_ERROR, "Name is already exists",
					"Name:" + name + " is already exists"));
		}
		
	}
	
	public void validateEmail(FacesContext context, UIComponent component,
			Object convertedValue) {
		String email = (String) convertedValue;
		if (email.indexOf("@")==-1 || email.indexOf(".")==-1) {
			throw new ValidatorException(new FacesMessage(
					FacesMessage.SEVERITY_ERROR, "Email address is invalid",
					"Email:" + email + " is invalid"));
		}
		
	}

	public boolean isValid(){
		if (password.equals(rpassword)) {
			return true;
			}
			return false;
	}
}
